package com.ssy.wireless.api.service;

import com.ssy.wireless.entity.ApDevice;
import com.ssy.wireless.entity.ApDeviceFault;
import com.ssy.wireless.mapper.ApDeviceFaultMapper;
import com.ssy.wireless.mapper.ApDeviceMapper;
import com.ssy.wireless.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * Created by cofey on 16/11/22.
 */
@Service
@Transactional(readOnly = true)
public class ApiApDeviceFaultService {

    @Autowired
    private ApDeviceMapper apDeviceMapper;

    @Autowired
    private ApDeviceFaultMapper apDeviceFaultMapper;

    @Transactional
    public void insert(String apIndex) {
        Assert.isTrue(StringUtils.hasLength(apIndex), "AP索引不能为空");
        ApDevice apDevice = apDeviceMapper.selectByApIndex(apIndex);
        Assert.notNull(apDevice, "当前AP索引[" + apIndex + "]不存在");
        ApDeviceFault fault = new ApDeviceFault();
        fault.setApDeviceId(apDevice.getId());
        fault.setIp(apDevice.getIp());
        fault.setMac(apDevice.getMac());
        fault.setJoinTime(apDevice.getJoinTime());
        fault.setEntId(apDevice.getEntId());
        fault.setHotspotId(apDevice.getHotspotId());
        fault.setFaultTime(new Date());
        apDeviceFaultMapper.insert(fault);

        apDeviceMapper.updateRunStatus(0, apDevice.getId(), apDevice.getHotspotId());
    }
}
